<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="8">
          <a-form-item label="标准物质名称" v-bind="validateInfos.name">
            <a-input v-model:value="formData.name" placeholder="请输入标准物质名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="标准物质类别" v-bind="validateInfos.type">
            <a-input v-model:value="formData.type" placeholder="请输入标准物质类别" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="标准物质批号" v-bind="validateInfos.batch">
            <a-input v-model:value="formData.batch" placeholder="请输入标准物质批号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="纯度/标准值" v-bind="validateInfos.purity">
            <a-input v-model:value="formData.purity" placeholder="请输入纯度/标准值" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="生产厂家" v-bind="validateInfos.factory">
            <a-input v-model:value="formData.factory" placeholder="请输入生产厂家" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="规格" v-bind="validateInfos.specs">
            <a-input v-model:value="formData.specs" placeholder="请输入规格" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="当前库存量" v-bind="validateInfos.inventoryNow">
	          <a-input-number v-model:value="formData.inventoryNow" placeholder="请输入当前库存量" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="单位" v-bind="validateInfos.unity">
            <a-input v-model:value="formData.unity" placeholder="请输入单位" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="归还入库量" v-bind="validateInfos.putNumber">
	          <a-input-number v-model:value="formData.putNumber"  style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="单位" v-bind="validateInfos.unit">
            <a-input v-model:value="formData.unit" placeholder="请输入单位" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="出库量" v-bind="validateInfos.outNumber">
	          <a-input-number v-model:value="formData.outNumber" placeholder="请输入出库量" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="单位" v-bind="validateInfos.units">
            <a-input v-model:value="formData.units" placeholder="请输入单位" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="领用业务id" v-bind="validateInfos.usedId">
            <a-input v-model:value="formData.usedId" placeholder="请输入领用业务id" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="标准物质id" v-bind="validateInfos.chemicalId">
            <a-input v-model:value="formData.chemicalId" placeholder="请输入标准物质id" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../LabChemicalUsedDescribe.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    name: '',   
    type: '',   
    batch: '',   
    purity: '',   
    factory: '',   
    specs: '',   
    inventoryNow: undefined,
    unity: '',   
    putNumber: undefined,
    unit: '',   
    outNumber: undefined,
    units: '',   
    usedId: '',   
    chemicalId: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    min-height: 500px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
